package peppa.george.book.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import peppa.george.base.Util.PageUtil;
import peppa.george.book.domain.Book;
import peppa.george.book.mapper.BookMapper;
import peppa.george.book.query.BookQuery;
import peppa.george.book.service.IBookService;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BookserviceImpl implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    public List<Book> findAll() {

        return bookMapper.findAll();
    }

    public Book findById(Long id) {

        return bookMapper.findById(id);
    }
    @Transactional
    public void save(Book book) {

        bookMapper.save(book);
    }
    @Transactional
    public void delete(Long id) {

        bookMapper.delete(id);
    }
    @Transactional
    public void update(Book book) {

        bookMapper.update(book);
    }

    public PageUtil<Book> queryByPage(BookQuery bookQuery) {
        Integer totals = bookMapper.queryByCount(bookQuery);
        if (totals == null || totals == 0){
            return new PageUtil<Book>(0,new ArrayList<Book>());
        }
        List<Book> books = bookMapper.queryByPage(bookQuery);
        return new PageUtil<>(totals,books);
    }
}
